package com.clss.commandhelper.function.supportTeam;

import com.clss.commandhelper.base.BaseCallback;
import com.clss.commandhelper.base.BaseJson;
import com.clss.commandhelper.base.BasePresenter;
import com.clss.commandhelper.base.BaseView;
import com.clss.commandhelper.tools.Constant;
import com.clss.commandhelper.tools.Utils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/11/30.
 */

public class SupportTeamListPresenter extends BasePresenter {

    SupportTeamListModel mModel;
    SupportView mSupportView;

    public void setSupportView(SupportView supportView) {
        mSupportView = supportView;
    }

    public void getPoliceList(){
        if (mModel == null){
            mModel = new SupportTeamListModel(mSupportView.getActContext());
        }
        mSupportView.showLoadingDialog();
        mModel.getPoliceList(new Callback<BaseJson>() {
            @Override
            public void onResponse(Call<BaseJson> call, Response<BaseJson> response) {
                mSupportView.dismissLoadingDialog();
                BaseJson baseJson = response.body();
                if (baseJson != null){
                    if (baseJson.getCode() != 0){
                        mSupportView.responseError(baseJson.getCode(), baseJson.getError());
                    }else {
                        Type type = new TypeToken<List<PoliceInfoBean>>(){}.getType();
                        List<PoliceInfoBean> beans = mGson.fromJson(baseJson.getList().toString(), type);
                        mSupportView.setListData(beans,baseJson.error);
                    }
                }else {
                    mSupportView.responseError(Constant.UNKNOW_ERROR_CODE, Constant.UNKNOW_ERROR_MESSAGE);
                }
            }

            @Override
            public void onFailure(Call<BaseJson> call, Throwable t) {
                mSupportView.dismissLoadingDialog();
                mSupportView.requestFailure(t);
            }
        });
//        mModel.getPoliceList(new BaseCallback(mSupportView) {
//            @Override
//            public void responseSuccess(BaseJson baseJson) {
//                Type type = new TypeToken<List<PoliceInfoBean>>(){}.getType();
//                List<PoliceInfoBean> beans = mGson.fromJson(baseJson.getList().toString(), type);
//                mSupportView.setListData(beans,baseJson.error);
//            }
//        });
    }

    public void askSupport(){
        List<String> policeIdList = mSupportView.getPoliceIds();
        if (Utils.isNull(policeIdList) || policeIdList.size() == 0){
            mSupportView.responseError(Constant.UNKNOW_ERROR_CODE,"请选择警员");
            return;
        }
        if (mModel == null){
            mModel = new SupportTeamListModel(mSupportView.getActContext());
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < policeIdList.size(); i++) {
            if (i != policeIdList.size()-1){
                stringBuilder.append(policeIdList.get(i)).append(",");
            }else {
                stringBuilder.append(policeIdList.get(i));
            }
        }
        mModel.askSupport(mSupportView.getId(), stringBuilder.toString(), new BaseCallback(mSupportView) {
            @Override
            public void responseSuccess(BaseJson baseJson) {
                mSupportView.requestSupport(baseJson.error);
            }
        });
    }

    @Override
    public void cancelCall() {

    }

    interface SupportView extends BaseView{
        String getId();
        List<String> getPoliceIds();
        void setListData(List<PoliceInfoBean> beans,String error);
        void requestSupport(String hint);
    }
}
